package com.boco.fznq.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.boco.fznq.R;
import com.boco.fznq.model.BannerModel;
import com.boco.fznq.webview.MyWebviewActivity;
import com.culiu.core.imageloader.CuliuImageLoader;
import com.culiu.core.widget.CustomImageView;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by jiangcheng on 2016/12/14.
 */

public class LoopBannerView extends RelativeLayout {

    /**
     * 开始时的延迟时间
     **/
    private static final int AUTO_SCROLL_INTERVAL = 5000;


    private BannerViewPager bannerViewPager;

    private LinearLayout mIndicatorContainer;

    private RelativeLayout mContainer;

    private List<BannerModel> mBannerList;

    private Context context;

    /**
     * 保存添加到ViewPager中的View的列表
     **/
    private ArrayList<CustomImageView> bannerViewList = new ArrayList<>();


    public LoopBannerView(Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public LoopBannerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }

    public LoopBannerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    private void initView() {
         inflate(getContext(), R.layout.view_loop_banner, this);
         bannerViewPager = (BannerViewPager) findViewById(R.id.banner_viewpager);


        //mContainer Params
        LayoutParams containerParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        containerParams.addRule(ALIGN_PARENT_BOTTOM);

        //indicatorContainerParams
        LayoutParams indicatorContainerParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        indicatorContainerParams.addRule(CENTER_IN_PARENT);

        //mContainer 外层容器（用于添加background）
        mContainer = new RelativeLayout(getContext());

        //存放指示器的容器
        mIndicatorContainer = new LinearLayout(getContext());
        mIndicatorContainer.setLayoutParams(indicatorContainerParams);
        int dp10 = getResources().getDimensionPixelSize(R.dimen.space_10_dp);
        mIndicatorContainer.setPadding(0, 0, dp10, dp10);

        mContainer.addView(mIndicatorContainer);
        addView(mContainer, containerParams);
    }

    /**
     *
     */
    public void fillView(List<BannerModel> bannerModels) {
        List<BannerModel> list = bannerModels;
        mBannerList = list;

        if (list == null || list.isEmpty()) {
            setVisibility(View.GONE);
            return;
        }

        if (list.size() > 1)
            addIndicatorView(list.size());

        addFirstAndLast(list);

        for (BannerModel banner : list)
            addBannerView(banner);
        setViewPagerAttributes();

    }


    private void addBannerView(final BannerModel banner) {
        CustomImageView view = (CustomImageView) LayoutInflater.from(getContext()).inflate(R.layout.imageview_fitxy, null);
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//                MyWebviewActivity.startMyWebView((Activity) context,
//                        "http://www.epicc.com.cn/m/");
            }
        });
        bannerViewList.add(view);
    }


    private void setViewPagerAttributes() {
        bannerViewPager.setOnPageChangeListener(new OnBannerPageChangeListener(bannerViewPager, mIndicatorContainer, bannerViewList, mBannerList));
        BannerPagerAdapter adapter = new BannerPagerAdapter(bannerViewList, mBannerList);
        bannerViewPager.setAdapter(adapter);

        if (mBannerList.size() > 1) {
            bannerViewPager.setOffscreenPageLimit(bannerViewList.size() / 2);
            bannerViewPager.setCurrentItem(1, false);
            final Timer timer = new Timer();
            final int delayTime = 3000;
            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    timer.cancel();
                    bannerViewPager.startAutoScroll();
                }
            }, delayTime);

            // scheduleViewPager();
        } else if (mBannerList.size() == 1) {
            bannerViewPager.setCurrentItem(0);
            if (mBannerList.get(0) != null) {
                CuliuImageLoader.getInstance().display(bannerViewList.get(0), mBannerList.get(0).getUrl());
            }
        }
    }

    private void addFirstAndLast(List<BannerModel> bannerList) {
        if (bannerList == null || bannerList.size() <= 1) {
            return;
        }

        BannerModel first = bannerList.get(0);
        BannerModel last = bannerList.get(bannerList.size() - 1);

        bannerList.add(0, last);
        bannerList.add(first);
    }

    private void addIndicatorView(int count) {
        for (int i = 0; i < count; i++) {
            ImageView indicatorView = new ImageView(getContext());
            indicatorView.setImageResource(R.drawable.viewpager_dot_white);
            indicatorView.setScaleType(ImageView.ScaleType.FIT_XY);
            mIndicatorContainer.addView(indicatorView, getIndicatorViewLayoutParams());
        }
    }

    private LinearLayout.LayoutParams getIndicatorViewLayoutParams() {
        LinearLayout.LayoutParams indicatorParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        indicatorParams.leftMargin = getResources().getDimensionPixelSize(R.dimen.space_3_dp);
        return indicatorParams;
    }


}
